# coding=utf-8

import zmq
import sys

ctx = zmq.Context.instance()
socket = ctx.socket(zmq.SUB)
socket.connect('tcp://localhost:5556')

zip_filter = sys.argv[1] if len(sys.argv) > 1 else "10001"

if isinstance(zip_filter, bytes):
    zip_filter = zip_filter.decode('ascii')

# zip filter like a channel
socket.setsockopt_string(zmq.SUBSCRIBE, zip_filter)

total_temp = 0

for update_nbr in range(1000):
    string = socket.recv_string()
    # data = socket.recv_json()
    # zipcode = data['zipcode']
    # temperature = data['temperature']
    # relhumidity = data['relhumidity']
    zipcode, temperature, relhumidity = string.split()
    total_temp += int(temperature)

    print 'average temperature for zipcode %s was %dF' % (zip_filter, total_temp / (update_nbr + 1))

socket.close()
ctx.destroy()
